package com.gree.scmservice.dao;

import com.gree.common.entity.TablePageData;
import com.gree.common.utils.StringUtils;
import com.gree.scmservice.entity.smcom.Notice;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * Created by 180686 on 2021/4/23 9:43
 */


@Repository
public class SysNoticeDao {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Dao dao;

    public TablePageData<Record> querySysNoticeList(Notice notice) {
        String sqlstr = "select styp, comp, xuno, capt, dsca, idat,(select duta from smcom04" +
                " where styp = t.styp and comp = t.comp and xuno = t.xuno) duta" +
                " from smcom01 t where styp = @styp and comp = @comp";
        if(StringUtils.isNotEmpty(notice.getCapt())){
            sqlstr+="  and capt like @capt";
        }
        sqlstr+= " order by idat desc";
        Sql sql = Sqls.create(sqlstr);
        sql.setParam("styp",notice.getStyp())
                .setParam("comp",notice.getComp())
                .setParam("capt","%"+notice.getCapt()+"%");
        Pager pager = dao.createPager(notice.getPageNum(), notice.getPageSize());
        sql.setPager(pager);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(Record.class));
        List<Record> dataList=dao.execute(sql).getList(Record.class);
        for(Record record:dataList){
            if(StringUtils.isNotEmpty(record.getString("duta"))){
                List list = Arrays.asList(record.getString("duta").split(","));
                record.set("duta",list);
            }
        }
        return new TablePageData(dataList, pager);
    }
}
